<script setup>

import {useLoginStore} from "@/sotre/useLoginStore.js";
import {onMounted, ref} from "vue";
import {reqInstance} from "@/utils/request/index.js";
import dayjs from "dayjs";
import AddEdit from "@/views/company/AddEdit.vue";
import {message} from "ant-design-vue";
onMounted(() => {
  getData()
})
const dataSource = ref([])
const getData = async () => {
  try {
    const res = await reqInstance({
      url: "/company",
      method: "get",
    })
    console.log("res",res);
    dataSource.value = (res?.data ?? []).map((item)=>{
      console.log("item",item);
      return item
    });
  } catch (e) {

  }
}

const delOne = async (id) => {
  try {
    await reqInstance({
      url: "/company/" + id,
      method: "delete",
    })
    message.success("删除成功")
    await getData()
  } catch (e) {

  }
}

const columns = [
  {
    title:"公司名称",
    dataIndex:"com_name",
  },
  {
    title:"社会统一信用代码",
    dataIndex:"com_code",
  },
  {
    dataIndex: 'action',
    title: '操作',
    key: 'action',
  },
]
const sonDrawRef = ref()
</script>

<template>
  <a-button type="primary" @click="sonDrawRef.openDraw(false)">新增</a-button>
  <AddEdit ref="sonDrawRef" @create-completed="getData()"/>
  <a-table :dataSource="dataSource" :columns="columns">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'action'">
        <a-button v-if="useLoginStore().isAdmin" class="edit-btn" @click="sonDrawRef.updateOpen(record)">编辑</a-button>
        <a-button v-if="useLoginStore().isAdmin" @click="delOne(record.id)">删除</a-button>
      </template>
    </template>
  </a-table>
</template>

<style scoped>

</style>